package com.wondertek.poms.sync.manager.main;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.core.service.IContentBlackService;
import com.wondertek.poms.core.service.IContentService;
import com.wondertek.poms.core.service.IContentWhiteService;
import com.wondertek.poms.core.service.IPcPrdInfoRuleService;
import com.wondertek.poms.core.service.IPcProductRuleService;
import com.wondertek.poms.core.service.IPrdContService;
import com.wondertek.poms.core.service.IPrdInfoService;
import com.wondertek.poms.core.service.IProductInfoPackageService;
import com.wondertek.poms.core.utils.OutHttpUtil;
import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.ContentBlack;
import com.wondertek.poms.dao.po.ContentWhite;
import com.wondertek.poms.dao.po.MoveContLog;
import com.wondertek.poms.dao.po.PrdCont;
import com.wondertek.poms.dao.po.PrdInfo;
import com.wondertek.poms.dao.po.PrdInfoRule;
import com.wondertek.poms.dao.po.ProductInfoPackage;
import com.wondertek.poms.dao.po.SyncTask;
import com.wondertek.poms.dao.repo.IPrdContDao;
import com.wondertek.poms.dao.repo.IPrdInfoDao;
import com.wondertek.poms.core.manager.NotifyBcManager;
import com.wondertek.poms.core.manager.ChargeManager;
import com.wondertek.poms.core.manager.EsManager;
import com.wondertek.poms.sync.manager.copyright.CopyrightManager;
import com.wondertek.poms.sync.manager.filter.RuleManager;
import com.wondertek.poms.sync.manager.mixed.MixedHttpUtils;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 * <b>ParentManager Description:</b> 自动入包流程父级
 * </p>
 *
 * @author douzi <b>DATE</b> 2019年8月20日 下午5:09:24
 */
@Slf4j
@Component
public class ParentManager {
    @Autowired
    public IProductInfoPackageService productInfoPackageService;
    @Autowired
    public IPrdInfoService prdInfoService;
    @Autowired
    public IContentService contentService;
    @Autowired
    public IPcProductRuleService productRuleService;
    @Autowired
    public IPrdContService prdContService;
    @Autowired
    public RuleManager ruleManager;
    @Autowired
    public OutHttpUtil outHttpUtil;
    @Autowired
    public IPcPrdInfoRuleService prdInfoRuleService;
    @Autowired
    public ChargeManager chargeManager;
    @Autowired
    public MixedHttpUtils mixedHttpUtils;
    @Autowired
    public NotifyBcManager notifyBcManager;
    @Autowired
    public IContentBlackService contentBlackService;
    @Autowired
    public IContentWhiteService contentWhiteService;
    @Autowired
    EsManager esManager;
    @Autowired
    CopyrightManager copyrightManager;
    @Autowired
    private IPrdInfoDao prdInfoDao;
    @Autowired
    private IPrdContDao prdContDao;
    
    /**
     * <p><b>Title:</b> getPrdInfoMap</p>
     * <p><b>Description:</b> 按照二级产品id 获取商品包集合,且商品包已发布</p>
     *
     * @return
     * @author douzi
     */
    public List<PrdInfo> getPrdInfo(Long productInfoPackageId) {
        List<PrdInfo> pis = prdInfoService.findPrdInfoInPackageId(productInfoPackageId, Constants.PRD_INFO_PUBLISH_STATUS_2);
        return pis;
    }
    
    /**
     * <p><b>Title:</b> getMoveInPrdInfo</p>
     * <p><b>Description:</b> 循环商品包，查看是否存在移入的商品包</p>
     *
     * @param c
     * @param secondPrdId
     * @return
     * @author douzi
     */
    public Long getMoveInPrdInfo(Content c, ProductInfoPackage pip) {
    	Long secondPrdId = pip.getProductInfoPackageId();
    	MoveContLog logs = outHttpUtil.findMoveLog(c.getContentId(), secondPrdId, true);
        if (logs != null) {
            log.info("主流程|二级产品:" + secondPrdId + "|下内容:" + c.getContentId() + "|曾经移入:" + logs.getNewPrdInfoId() + "商品包");
            return logs.getNewPrdInfoId();
        } else {
            log.info("主流程|二级产品:" + secondPrdId + "|下内容:" + c.getContentId() + "|没有移入任何商品包");
            return null;
        }
    }
    
    /**
     * <p><b>Title:</b> getWhichPrdInfo</p>
     * <p><b>Description:</b> 获取符合哪个商品包，条件有： 包限制条件、包已选条件</p>
     *
     * @param vo
     * @param pis
     * @return
     * @author douzi
     */
    public Long getWhichPrdInfo(Content c, List<PrdInfo> pis, ProductInfoPackage pip) {
        log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|入包二级产品:" + pip.getProductInfoPackageId() + "|商品包限制条件、包已选条件 判断开始");
        for (Iterator<PrdInfo> iterator = pis.iterator(); iterator.hasNext(); ) {
            PrdInfo prdInfo = iterator.next();
            Long prdInfoId = prdInfo.getPrdInfoId();
            
            Map<Integer, List<PrdInfoRule>> pirMap = getPrdInfoRuleMap(prdInfoId);
            List<PrdInfoRule> limitPrdInfoRules = pirMap.get(Constants.RULE_TYPE_4);
            if (CollectionUtil.isNotEmpty(limitPrdInfoRules)) {
                log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|入包二级产品:" + pip.getProductInfoPackageId() + "|商品包:" + prdInfoId
                        + "|限制规则数量:" + limitPrdInfoRules.size());
                if (isMatchPrdInfoLimitRule(c, limitPrdInfoRules))
                    continue;
            }

            List<PrdInfoRule> selectPrdInfoRules = pirMap.get(Constants.RULE_TYPE_2);
            if (selectPrdInfoRules == null)
                continue;
            log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|入包二级产品:" + pip.getProductInfoPackageId() + "|商品包:" + prdInfoId + "|已选规则数量:"
                    + selectPrdInfoRules.size());
            if (!isMatchPrdInfoSelectRule(c, selectPrdInfoRules))
                continue;

            return prdInfoId;
        }
        return null;
    }
    
    /**
     * <p><b>Title:</b> getWhichPrdInfo</p>
     * <p><b>Description:</b> 获取符合哪个商品包，条件有： 包限制条件、包已选条件</p>
     *
     * @param vo
     * @param pis
     * @return
     * @author douzi
     */
    public Long getWhichPrdInfo(Content c, List<PrdInfo> pis) {
        log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|商品包限制条件、包已选条件 判断开始");
        for (int i = pis.size() - 1; i >= 0; i --) {
            PrdInfo prdInfo = pis.get(i);
            Long prdInfoId = prdInfo.getPrdInfoId();
            
            Map<Integer, List<PrdInfoRule>> pirMap = getPrdInfoRuleMap(prdInfoId);
            List<PrdInfoRule> limitPrdInfoRules = pirMap.get(Constants.RULE_TYPE_4);
            if (CollectionUtil.isNotEmpty(limitPrdInfoRules)) {
                log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|商品包:" + prdInfoId
                        + "|限制规则数量:" + limitPrdInfoRules.size());
                if (isMatchPrdInfoLimitRule(c, limitPrdInfoRules))
                    continue;
            }

            List<PrdInfoRule> selectPrdInfoRules = pirMap.get(Constants.RULE_TYPE_2);
            if (selectPrdInfoRules == null)
                continue;
            log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|商品包:" + prdInfoId + "|已选规则数量:"
                    + selectPrdInfoRules.size());
            if (!isMatchPrdInfoSelectRule(c, selectPrdInfoRules))
                continue;

            return prdInfoId;
        }
        return null;
    }
    
    /**
     * <p><b>Title:</b> getPrdInfoRules</p>
     * <p><b>Description:</b> 获取符合哪个商品包，条件有： 包限制条件、包已选条件</p>
     * @author douzi
     * @param c
     * @param pi
     * @return
     */
    public Long getPrdInfoRules(Content c, PrdInfo pi) {
    	log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|商品包:"+ pi.getPrdInfoId() + "|包名称:" + pi.getName() + "|包限制条件、包已选条件 判断开始");
    	Map<Integer, List<PrdInfoRule>> pirMap = getPrdInfoRuleMap(pi.getPrdInfoId());
        List<PrdInfoRule> limitPrdInfoRules = pirMap.get(Constants.RULE_TYPE_4);
        if (CollectionUtil.isNotEmpty(limitPrdInfoRules)) {
            log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|商品包:" + pi.getPrdInfoId() + "|限制规则数量:" + limitPrdInfoRules.size());
            if (isMatchPrdInfoLimitRule(c, limitPrdInfoRules))
                return null;
        } else {
        	log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|商品包:" + pi.getPrdInfoId() + "|限制规则为空");
        }

        List<PrdInfoRule> selectPrdInfoRules = pirMap.get(Constants.RULE_TYPE_2);
        if (CollectionUtil.isNotEmpty(selectPrdInfoRules)) {
        	log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|商品包:" + pi.getPrdInfoId() + "|已选规则数量:" + selectPrdInfoRules.size());
            if (!isMatchPrdInfoSelectRule(c, selectPrdInfoRules))
            	return null;
        } else {
        	log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|商品包:" + pi.getPrdInfoId() + "|已选规则为空");
        	return null;
        }

        return pi.getPrdInfoId();
	}
    
    /**
     * <p><b>Title:</b> subSetDelProcess</p>
     * <p><b>Description:</b> 删除子集流程</p>
     *
     * @param vo 内容对象
     * @author douzi
     */
    public void mainDelProcess(SyncTask st, String model) {
    	log.info(model + "|删除子集流程|媒资ID:" + st.getAssetId() + "|内容:" + st.getContentId());
        List<PrdCont> prdContList = prdContService.getPrdContByMmsId(st.getAssetId());
        if (prdContList != null && prdContList.size() > 0) {
            for (PrdCont pc : prdContList) {
                delPrdCont(st, pc, model);
            }
        }
    }
    
    /**
     * <p><b>Title:</b> delPrdCont</p>
     * <p><b>Description:</b> 删除节目，并且下线节目</p>
     * @param st
     * @param prdCont
     * @author douzi
     */
    public void delPrdCont(SyncTask st, PrdCont prdCont, String model) {
//        PrdInfo prdInfo = prdInfoService.findById(prdCont.getPrdInfoId());
        prdCont.setPubStatus(Constants.PUB_STATUS_22);
        prdCont.setPublishNoVOMSStatus(Constants.PUB_STATUS_22);
        prdCont.setPublishNoVOMSTime(new Date());
        prdCont.setIsDelete(Constants.DELETE_1);
        PrdCont.putTimeAndUser(prdCont);
        prdContService.save(prdCont);
        // 需要调用下线节目的接口 
        outHttpUtil.offline(prdCont, model);
    }
    
    /**
     * <p><b>Title:</b> getPrdInfoRuleMap</p>
     * <p><b>Description:</b> 根据商品包id查询商品包规则，并且按照限制和已选进行分类</p>
     *
     * @param prdInfoId
     * @return
     * @author douzi
     */
    public Map<Integer, List<PrdInfoRule>> getPrdInfoRuleMap(Long prdInfoId) {
        Map<Integer, List<PrdInfoRule>> map = new HashMap<>();
        List<PrdInfoRule> pirs = prdInfoRuleService.findAllByPrdInfoId(prdInfoId);

        pirs.forEach(r -> {
            List<PrdInfoRule> partPirs = null;
            if (!map.containsKey(r.getIsprdSel())) {
                partPirs = new ArrayList<>();
            } else {
                partPirs = map.get(r.getIsprdSel());
            }

            partPirs.add(r);
            map.put(r.getIsprdSel(), partPirs);
        });

        return map;
    }
    
    /**
     * <p><b>Title:</b> isMatchPrdInfoLimitRule</p>
     * <p><b>Description:</b> 是否匹配商品包限制规则</p>
     *
     * @param vo
     * @param limitPrdInfoRules
     * @return
     * @author douzi
     */
    public boolean isMatchPrdInfoLimitRule(Content c, List<PrdInfoRule> limitPrdInfoRules) {
        boolean isMatch = false;

        for (PrdInfoRule limitRule : limitPrdInfoRules) {
            isMatch = ruleManager.contentFilter(limitRule, c, true);
            if (isMatch)
                break;
        }
        return isMatch;
    }
    
    /**
     * <p><b>Title:</b> isMatchPrdInfoSelectRule</p>
     * <p><b>Description:</b> 是否匹配商品包已选规则</p>
     *
     * @param vo
     * @param selectPrdInfoRules
     * @return
     * @author douzi
     */
    public boolean isMatchPrdInfoSelectRule(Content c, List<PrdInfoRule> selectPrdInfoRules) {
        boolean isMatch = false;

        for (PrdInfoRule selectRule : selectPrdInfoRules) {
            isMatch = ruleManager.contentFilter(selectRule, c, false);
            if (isMatch)
                break;
        }
        return isMatch;
    }
    
    /**
     * <p><b>Title:</b> getPrdInfoIdExist</p>
     * <p><b>Description:</b> 判断最新包是否存在</p>
     *
     * @param newPrdInfoId
     * @param pis
     * @return
     * @author douzi
     */
    public Long getPrdInfoIdExist(Long newPrdInfoId, List<PrdInfo> pis) {
        for (Iterator<PrdInfo> iterator = pis.iterator(); iterator.hasNext(); ) {
            PrdInfo prdInfo = iterator.next();
            Long prdInfoId = prdInfo.getPrdInfoId();

            if (newPrdInfoId.equals(prdInfoId)) {
                return newPrdInfoId;
            }
        }

        log.info("主流程|曾经移入:" + newPrdInfoId + "商品包状态撤回");
        return null;
    }
    
    
    /**
     * <p><b>Title:</b> offineContent</p>
     * <p><b>Description:</b> 下线多个节目</p>
     *
     * @param pcs
     * @author douzi
     */
    protected void offineContents(List<PrdCont> pcs, List<PrdInfo> piList, Content c, String model) {
        for (int i = 0; i < pcs.size(); i++) {
        	PrdCont pcDb = pcs.get(0);
        	//只有相同内容时，需要下线；其他同媒资内容不能下线
        	if (pcDb.getContentId().equals(c.getContentId())) {
        		PrdCont cpi = pcs.get(i);
                PrdInfo prdInfo = getPrdInfo(piList, cpi.getPrdInfoId());
                offineContent(cpi, prdInfo, c, model);
        	}
        }
    }
    
    /**
     * <p><b>Title:</b> offineContent</p>
     * <p><b>Description:</b> 下线多个节目</p>
     *
     * @param pcs
     * @author douzi
     */
    protected void offineContents(List<PrdCont> pcs, String model) {
        for (int i = 0; i < pcs.size(); i++) {
            PrdCont cpi = pcs.get(i);
            offineContent(cpi, model);
        }
    }
    
    /**
     * <p><b>Title:</b> offineContent</p>
     * <p><b>Description:</b> 删除下线单个节目</p>
     *
     * @param pc
     * @param prdInfo
     * @author douzi
     */
    protected void offineContent(PrdCont pc, PrdInfo prdInfo, Content c, String model) {
        // 下线节目, 暂时需要调用老POMS接口实现、暂时走定时接口
    	log.info(model + "|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|节目id：" + pc.getPrdContId() + "|入包二级产品:" + prdInfo.getProductInfoPackageId() + "|下线单个节目:" + pc.toString());
        pc.setIsDelete(Constants.DELETE_1);
        PrdCont.putTimeAndUser(pc);
        prdContService.save(pc);
        // 需要调用下线节目的接口 
        outHttpUtil.offline(pc, model);
    }
    
    /**
     * <p><b>Title:</b> offineContent</p>
     * <p><b>Description:</b> 删除下线单个节目</p>
     *
     * @param pc
     * @param prdInfo
     * @author douzi
     */
    protected void offineContent(PrdCont pc, String model) {
        // 下线节目, 暂时需要调用老POMS接口实现、暂时走定时接口
    	log.info(model + "|内容:" + pc.getContentId() + "|节目id：" + pc.getPrdContId() + "|下线单个节目");
        pc.setIsDelete(Constants.DELETE_1);
        PrdCont.putTimeAndUser(pc);
        prdContService.save(pc);
        // 需要调用下线节目的接口 
        outHttpUtil.offline(pc, model);
    }
    
    /**
     * <p><b>Title:</b> getPrdInfo</p>
     * <p><b>Description:</b> 获取到商品包对象</p>
     *
     * @param piList
     * @param newPrdInfoId 新商品包id
     * @return
     * @author douzi
     */
    public PrdInfo getPrdInfo(List<PrdInfo> piList, Long newPrdInfoId) {
        for (PrdInfo pi : piList) {
            if (pi.getPrdInfoId().equals(newPrdInfoId)) {
                return pi;
            }
        }
        return null;
    }
    
    /**
     * <p><b>Title:</b> inPrdInfo</p>
     * <p><b>Description:</b> 入包主业务</p>
     * 增加内容id不变，节目id不变业务；由于poms数据库中存在多个已删除节目id，使用最小id为准
     *
     * @param prdInfo 商品包
     * @param vo      接收内容对象
     * @author douzi
     */
    public void inPrdInfo(PrdInfo prdInfo, Content c, PrdCont prdCont) {
        PrdCont cipi = null;
        if (prdCont == null) {
            cipi = initPrdCont(c, prdInfo);
        } else {
            cipi = udpatePrdCont(c, prdInfo, prdCont);
        }

        chargeManager.chargePrdCont(cipi, prdInfo, c);
        chargeManager.matchEquityId(cipi, prdInfo, c);
        prdContService.save(cipi);

        // 最新集免费
//        Integer serialSequence = c.getSerialSequence();
//        Long serialAssetId = c.getSerialAssetId(); // 壳的媒资ID
//        if (null != serialSequence && null != serialAssetId) { // 正片子集才进行限免操作，忽略预告片等。壳的ID为空，忽略
//            newSubFreeManager.subFreeManager(prdCont, prdInfo, serialAssetId);
//        }
        esManager.process(cipi);
        prepareNotify(prdInfo, c, cipi);
        // 混编通知
        mixedHttpUtils.mixedCharge(cipi, prdInfo);
    }
    
    /**
     * <p><b>Title:</b> initPrdCont</p>
     * <p><b>Description:</b> 新增时初始化节目对象</p>
     *
     * @param vo
     * @param prdInfo
     * @return
     * @author douzi
     */
    public PrdCont initPrdCont(Content c, PrdInfo prdInfo) {
        PrdCont pc = new PrdCont();
        pc.setPrdInfoId(prdInfo.getPrdInfoId());
        pc.setContentId(c.getContentId());
        pc.setMmsId(Long.parseLong(c.getAssetId()));
        pc.setCopyRightObjectID(Long.parseLong(c.getCopyrightObjectId()));
        pc.setBcStatus(Constants.BC_STATUS_0);
        pc.setBcId(null);
        pc.setPubStatus(Constants.PUB_STATUS_10);
        pc.setFileAction(NumberUtils.toInt(c.getStatus() + ""));
        pc.setFormType(c.getFormType());
        pc.setIsEmergency(c.getBcPriority());
        pc.setLongtitle(c.getName());
        pc.setShorttitle(c.getShortName());
        pc.setIsShowPrdId("1");
        pc.setPrice(c.getPricing());
        pc.setPublishNoVOMSStatus(Constants.PRD_CONT_PUBLISH_STATUS_10);

        /** 保留字段开始，无用 */
        pc.setDistStatus(Constants.DISTON_STATUS_10);
        pc.setRecall(Constants.RECALL_STATUS_0);
        pc.setIsEmergency(Constants.EMERGENCY_0);
        pc.setIsDelete(0); //需要保证所有数据为未删除
        if (StringUtils.isNotBlank(c.getBcId())) {
            try {
                pc.setBcId(Long.valueOf(c.getBcId().trim()));
            } catch (Exception e) {
            }
        }
        /** 保留字段结束，无用 */
        if ((c.getFormType() != null && c.getFormType().trim().equals("6"))
                || (c.getCategory() != null && c.getCategory().trim().equals("3"))) {
            pc.setDistStatus(Constants.DISTON_STATUS_12);
        }
        // if (vo.getFormType() != null && vo.getFormType().trim().equals("9"))
        // {// 专辑
        // pc.setBcStatus(Constants.BC_STATUS_1);
        // pc.setDistStatus(Constants.DISTON_STATUS_12);
        // pc.setPubStatus(Constants.PUB_STATUS_30);
        // }
        // if (vo.getFormType() != null && vo.getFormType().trim().equals("14"))
        // {// 播单
        //// pc.setBcStatus(Constants.BC_STATUS_1);
        // pc.setDistStatus(Constants.DISTON_STATUS_12);
        // pc.setPubStatus(Constants.PUB_STATUS_30);
        // }
        if (c.getHasVideo() != null && (c.getHasVideo()).equals(0)) {// 兼容媒资
            pc.setDistStatus(Constants.DISTON_STATUS_12);
        }

        PrdCont.putTimeAndUser(pc);
        
		// 保证节目id唯一 高正行待验证
//		Long productInfoPackageId = prdInfoDao.findProductInfoPackageId(prdInfo.getPrdInfoId());
//		// 没有二级产品的产品(商品)包
//		if (productInfoPackageId == null || 0L == productInfoPackageId) {
//			List<PrdCont> onList = prdContDao.findPcByContentIdInPrdInfo(c.getContentId(), prdInfo.getPrdInfoId());
//			if (onList.size() > 0) {
//				pc.setPrdContId(onList.get(0).getPrdContId());
//				if (onList.size() > 1) {
//					onList.remove(0);
//					offineContents(onList, "普通包or父子包|新增节目时|多条合法节目存在");
//				}
//			} else {
//				List<PrdCont> delList = prdContDao.findAllPcByContentIdInPrdInfo(c.getContentId(),
//						prdInfo.getPrdInfoId());
//				if (delList.size() > 0) {
//					pc.setPrdContId(delList.get(0).getPrdContId());
//				}
//			}
//		} else { // 有二级产品的产品(商品)包
//			List<PrdCont> onList = prdContDao.findPcByContentIdInPackageId2(c.getContentId(), productInfoPackageId);
//			if (onList.size() > 0) {
//				pc.setPrdContId(onList.get(0).getPrdContId());
//				if (onList.size() > 1) {
//					onList.remove(0);
//					offineContents(onList, "二级产品|新增节目时|多条合法节目存在");
//				}
//			} else {
//				List<PrdCont> delList = prdContDao.findAllPcByContentIdInPackageId2(c.getContentId(),
//						productInfoPackageId);
//				if (delList.size() > 0) {
//					pc.setPrdContId(delList.get(0).getPrdContId());
//				}
//			}
//		}
        
        log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|入包二级产品:" + prdInfo.getProductInfoPackageId() + "|初始化节目对象:" + pc.toString());
        return pc;
    }
    
    /**
     * <p><b>Title:</b> udpatePrdCont</p>
     * <p><b>Description:</b> 变更时修改节目对象</p>
     *
     * @param vo
     * @param prdInfo
     * @param pc
     * @return
     * @author douzi
     */
    public PrdCont udpatePrdCont(Content c, PrdInfo prdInfo, PrdCont pc) {
        pc.setPrdInfoId(prdInfo.getPrdInfoId());
        pc.setContentId(c.getContentId());
        pc.setFileAction(NumberUtils.toInt(c.getStatus() + ""));
        pc.setBcId(NumberUtils.toLong(c.getBcId()));
        pc.setCopyRightObjectID(Long.parseLong(c.getCopyrightObjectId()));
        pc.setFormType(c.getFormType());
        pc.setIsEmergency(c.getBcPriority());
        pc.setIsShowPrdId("1");
        pc.setLongtitle(c.getName());
        pc.setShorttitle(c.getShortName());
        pc.setPrice(c.getPricing());
        pc.setBcStatus(Constants.BC_STATUS_0);
        pc.setBcId(null);
        pc.setPubStatus(Constants.PUB_STATUS_10);
        pc.setPublishNoVOMSStatus(Constants.PRD_CONT_PUBLISH_STATUS_10);
        pc.setIsDelete(0); //需要保证所有数据为未删除
        // pc.setLevelval(levelval);
        // pc.setOrderFlag(vo.get);
        // pc.setPushStatus();
        // pc.setRankval();
        // pc.setRecall(recall);
        // pc.setSalCategory(salCategory);
        // pc.setStartTime(startTime);
        PrdCont.putTimeAndUser(pc);
        log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|入包二级产品:" + prdInfo.getProductInfoPackageId() + "|变更节目对象:" + pc.toString());
        return pc;
    }
    
    /**
     * <p><b>Title:</b> subSetToNewPrdInfo</p>
     * <p><b>Description:</b> 如果是壳，需要让子集跟着一起去新包</p>
     *
     * @param newPrdInfoId
     * @param prdContDbs
     * @param vo
     * @author douzi
     */
    public void subSetToNewPrdInfo(Long newPrdInfoId, List<PrdCont> prdContDbs, Content c) {
        if (StringUtils.isNotBlank(c.getFormType())
                && (("6").equals(c.getFormType()) || ("10").equals(c.getFormType()) 
                || ("14").equals(c.getFormType()) || ("15").equals(c.getFormType()))) {
            if (prdContDbs.size() < 1) {
                log.info("主流程|子集跟随壳变更|DB中不存在壳的节目");
                return;
            }

            PrdCont pc = prdContDbs.get(0);
            if (newPrdInfoId.equals(pc.getPrdInfoId())) {
                log.info("主流程|子集跟随壳变更|壳的包没有变更");
                return;
            }

            List<PrdCont> subPrdConts = prdContService.getSerialsAllPublish(pc.getPrdInfoId(), pc.getMmsId());
            if (CollectionUtils.isEmpty(subPrdConts)) {
                log.info("主流程|子集跟随壳变更|壳没有子集存在");
                return;
            }

            for (PrdCont subPc : subPrdConts) {
            	// 走定时发布
                subPc.setPrdInfoId(newPrdInfoId);
                subPc.setPublishNoVOMSStatus(Constants.PRD_CONT_PUBLISH_STATUS_10);
                PrdCont.putTimeAndUser(subPc);
                prdContService.save(subPc);
            }
        }
    }
    
    /**
     * <p><b>Title:</b> prepareNotify</p>
     * <p><b>Description:</b> 根据先上后审、免审、先审后上 准备推送流程</p>
     *
     * @param vo
     * @param prdCont
     * @author douzi
     */
    public void prepareNotify(PrdInfo prdInfo, Content c, PrdCont prdCont) {
    	log.info("主流程|媒资ID:" + c.getAssetId() + "|superBcType:" + c.getSuperBcType());
    	if (c.getSuperBcType() == null) {
    		c.setSuperBcType(Constants.SUPER_BC_TYPE_2);
    	}
        switch (c.getSuperBcType()) {
            case Constants.SUPER_BC_TYPE_1:
                outHttpUtil.online(prdCont);
                notifyBcManager.process(prdInfo, c, prdCont);
                break;
            case Constants.SUPER_BC_TYPE_0:
            case Constants.SUPER_BC_TYPE_2:
                notifyBcManager.process(prdInfo, c, prdCont);
                break;
            case Constants.SUPER_BC_TYPE_3:
                innerBc(prdCont);
                outHttpUtil.online(prdCont);
                // if big screen, must broadcast control
//                ProductInfoPackage pip = productInfoPackageService.get(prdInfo.getProductInfoPackageId());
//                if (pip != null) {
//                    if (pip.getProductInfoPackageType() == 1) {
//                        notifyBcManager.process(prdInfo, c, prdCont);
//                    }
//                } else {
//                }
                break;
            default:
                log.info("媒资:" + c.getAssetId() + "|内容:" + c.getContentId() + "|不属于任何一种流程标记（先上后审、免审、先审后上）：" + c.getSuperBcType());
                break;
        }
    }
    
    /**
     * <p><b>Title:</b> innerBc</p>
     * <p><b>Description:</b> 内部播控修改状态</p>
     *
     * @param pc
     * @author douzi
     */
    void innerBc(PrdCont pc) {
        pc.setBcStatus(Constants.BC_STATUS_1);
        pc.setBcTime(new Date());
        pc.setBcPerson("INNER_BC");
        pc.setPublishNoVOMSStatus(Constants.PUB_STATUS_10);
        PrdCont.putTimeAndUser(pc);
        prdContService.save(pc);
    }
    
    /**
	 * <p><b>Title:</b> newLive</p>
	 * <p><b>Description:</b> 新增直播节目业务</p>
	 * @author douzi
	 * @param c
	 * @param parentPc
	 */
	public void newLive(Content c, PrdCont parentPc) {
		PrdInfo prdInfo = prdInfoService.get(parentPc.getPrdInfoId());
		if (prdInfo != null && prdInfo.getIsPublish() == 2 && 
				(prdInfo.getPrdInfoType() == null || prdInfo.getPrdInfoType() != 3)) {
			PrdCont pc = initPrdCont(c, prdInfo);
			pc.setBcStatus(Constants.BC_STATUS_1);
			chargeManager.chargePrdCont(pc, prdInfo, c);
			if (c.getRecommendation() == null || "".equals(c.getRecommendation())) {
				chargeManager.setParentEquityId(pc, parentPc);
			} else {
				chargeManager.matchEquityId(pc, prdInfo, c);
			}
			prdContService.save(pc);
			log.info("直播节目主流程|媒资:" + c.getAssetId() + "|内容:" + c.getContentId() + "|直播节目新增成功");
		}
	}

	/**
	 * <p><b>Title:</b> updateLive</p>
	 * <p><b>Description:</b> 更新当前直播节目为待发布</p>
	 * @author douzi
	 * @param c
	 * @param parentPc
	 * @param dbPcs
	 */
	public void updateLive(Content c, PrdCont parentPc, List<PrdCont> dbPcs) {
		PrdCont dbPc = dbPcs.get(0);
		PrdInfo prdInfo = prdInfoService.get(dbPc.getPrdInfoId());
		dbPc = udpatePrdCont(c, prdInfo, dbPc);
		dbPc.setBcStatus(Constants.BC_STATUS_1);
		
		if ((Integer.valueOf(1).equals(prdInfo.getAutoVomsPublish()) && dbPc.getPublishNoVOMSStatus() == null)
		        || (dbPc.getPublishNoVOMSStatus() != null && dbPc.getPublishNoVOMSStatus() != 22)) {
			dbPc.setPublishNoVOMSStatus(10);//新发布
		}
		
		chargeManager.chargePrdCont(dbPc, prdInfo, c);
		if (c.getRecommendation() == null || "".equals(c.getRecommendation())) {
			chargeManager.setParentEquityId(dbPc, parentPc);
		} else {
			chargeManager.matchEquityId(dbPc, prdInfo, c);
		}
		prdContService.save(dbPc);
		log.info("直播节目主流程|媒资:" + c.getAssetId() + "|内容:" + c.getContentId() + "|直播节目更新成功");
	}
	
	/**
	 * <p><b>Title:</b> delOtherPrdInfo</p>
	 * <p><b>Description:</b> 删除其他包中的直播节目或者子集</p>
	 * @author douzi
	 * @param c
	 * @param inPrdInfo
	 * @param piList
	 */
	public void delOtherPrdInfo(Content c, PrdInfo inPi, List<PrdInfo> piList) {
		piList.stream().parallel().forEach(pi -> {
			if (!inPi.getPrdInfoId().equals(pi.getPrdInfoId())) {
				List<PrdCont> dbPcs = prdContService.findPcByContentIdInPrdInfo(c.getContentId(), pi.getPrdInfoId());
				if (dbPcs != null && dbPcs.size() > 0) {
					for (int i = 0; i < dbPcs.size(); i++) {
						offineContent(dbPcs.get(i), pi, c, "normal");
					}
				}
			}
		});
	}
	
	/**
     * <p><b>Title:</b> filterWhite</p>
     * <p><b>Description:</b> 移入日志判断修改为白名单</p>
     * @author douzi
     * @param c
     * @param pi
     * @return
     */
	public Long filterWhite(Content c, PrdInfo pi) {
		ContentWhite params = new ContentWhite();
        params.setContentId(c.getContentId());
        params.setPrdInfoId(pi.getPrdInfoId());
        List<ContentWhite> cws = contentWhiteService.findAll(params);
        log.info("普通包/父子包流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|入包:" + pi.getPrdInfoId() + "|白名单包数量:" + cws.size());
        if (cws == null || cws.size() <= 0) {
        	return null;
        }
        log.info("普通包/父子包流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|入包:" + pi.getPrdInfoId() + "|白名单包:" + JSONUtil.toJsonStr(cws));
        return cws.get(0).getPrdInfoId();
	}

	/**
	 * <p><b>Title:</b> filterBlack</p>
	 * <p><b>Description:</b> 过滤黑名单</p>
	 * @author douzi
	 * @param c
	 * @param pip
	 * @param prdInfos
	 * @return
	 */
	public boolean filterBlack(Content c, PrdInfo pi) {
		ContentBlack params = new ContentBlack();
        params.setContentId(c.getContentId());
        params.setPrdInfoId(pi.getPrdInfoId());
        List<ContentBlack> cbs = contentBlackService.findAll(params);
        log.info("普通包or父子包流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|入包:" + pi.getPrdInfoId() + "|黑名单包数量:" + cbs.size());
		return cbs != null && cbs.size() > 0;
	}
	
	/**
	 * <p><b>Title:</b> findOptimalPrdCont</p>
	 * <p><b>Description:</b> 同媒资判断业务流程</p>
	 * @author douzi
	 * @param c
	 * @param prdInfo
	 * @param pcDb
	 */
	public void findOptimalPrdCont(Content c, PrdInfo prdInfo, PrdCont pcDb, String model) {
		Content nbc = copyrightManager.process(pcDb, c, prdInfo);
		if (nbc.getContentId().equals(pcDb.getContentId())) {
			inPrdInfo(prdInfo, nbc, pcDb);
		} else {
			offineContent(pcDb, prdInfo, c, model);
			inPrdInfo(prdInfo, c, null);
		}
	}
	
	/**
	 * <p><b>Title:</b> findOptimalPrdCont</p>
	 * <p><b>Description:</b> 同媒资判断业务流程</p>
	 * @author douzi
	 * @param c
	 * @param parentPc
	 * @param prdInfo
	 * @param prdContDbs
	 * @param pcDb
	 */
	public void findOptimalPrdCont(Content c, PrdCont parentPc, PrdInfo prdInfo, 
			List<PrdCont> prdContDbs, PrdCont pcDb, String model) {
		Content nbc = copyrightManager.process(pcDb, c, prdInfo);
		if (nbc.getContentId().equals(pcDb.getContentId())) {
			updateLive(nbc, parentPc, prdContDbs);
		} else {
			offineContent(pcDb, prdInfo, c, "主流程-直播节目");
			newLive(c, parentPc);
		}
	}
}
